using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WP.Commons;
using WP.DB;

namespace WP.Modules
{
    public partial class frmGetOrdNo : BaseQryRpt 
    {
        public frmGetOrdNo()
        {
            InitializeComponent();
        }

        private int mintModule;
        private string mstrSupOrCustNo = "";
        private bool mblnCancel = true;
        private bool mblnExistingOrdNo = false;
        public enum mModuleType
        {
            SalesOrderNo = 0, DailyRoute, PurchaseOrdNo
        }

      
        public bool GetNewOrdNo(ref string strNewOrdNo, int intModule, ref string strSupOrCustNo)
        {
            return GetNewOrdNo(ref strNewOrdNo, intModule,ref strSupOrCustNo, true, false, false);
        }
        public bool GetNewOrdNo(ref string strNewOrdNo, int intModule, ref string strSupOrCustNo, bool blnGetOrdNo, bool blnGetSupOrCustNo, bool blnShowPromptInfo)
        {
            bool blnExistingOrdNo = false;
            return GetNewOrdNo(ref strNewOrdNo, intModule, ref strSupOrCustNo, blnGetOrdNo, blnGetSupOrCustNo, blnShowPromptInfo, ref blnExistingOrdNo);
        }
        public bool GetNewOrdNo(ref string strNewOrdNo, int intModule, ref string strSupOrCustNo, bool blnGetOrdNo, bool blnGetSupOrCustNo, bool blnShowPromptInfo, ref bool blnExistingOrdNo)
        {

            mblnCancel = true;
            mintModule = intModule;
            mstrSupOrCustNo = strSupOrCustNo;
            
            txtOrdNo.Text = strNewOrdNo;
            txtSupOrCust.Text = strSupOrCustNo;
            string strOrigNewOrdNo = strNewOrdNo;
            bool blnExist = false;

            switch (mintModule)
            {
                case (int)mModuleType.SalesOrderNo:
                    blnExist = (DataFunctions.CheckRecordExist("oeOrderHdr", strNewOrdNo, "OrdNO"));
                    break;
                case (int)mModuleType.PurchaseOrdNo:
                    blnExist = (DataFunctions.CheckRecordExist("poorderhdr", strNewOrdNo, "OrdNo"));
                    break;
                default:
                    blnExist = false;
                    break;
            }

            if (blnGetOrdNo)
            {
                lblOrdNo.Visible = true;
                txtOrdNo.Visible = true;
                ubtOrdNo.Visible = true;
            }
            else
            {
                lblOrdNo.Visible = false;
                txtOrdNo.Visible = false;
                ubtOrdNo.Visible = false;
            }

            if (blnGetSupOrCustNo)
            {
                lblSupOrCust.Visible = true;
                txtSupOrCust.Visible = true;
                ubtSupOrCust.Visible = true;
            }
            else
            {
                lblSupOrCust.Visible = false;
                txtSupOrCust.Visible = false;
                ubtSupOrCust.Visible = false;
            }

            if (!blnShowPromptInfo)
            {
                lblPrompt.Visible = false;
                pictureBox1.Visible = false;
            }
            else
            {
                lblPrompt.Visible = true;
                pictureBox1.Visible = true;

                switch (mintModule)
                {
                    case (int)mModuleType.SalesOrderNo:
                        lblPrompt.Text = "Transfer Now?";
                        break;
                    default:
                        lblPrompt.Text = "";
                        break;
                }
                
            }

            switch (mintModule)
            {
                case (int)mModuleType.SalesOrderNo:
                    lblOrdNo.Text = "New Order#:";
                    break;
                default:
                    lblOrdNo.Text = "New Order#:";
                    break;
            }


            this.ShowDialog();
            this.Close();

            strNewOrdNo = txtOrdNo.Text;
            strSupOrCustNo = txtSupOrCust.Text;

            blnExistingOrdNo = mblnExistingOrdNo;
            if (strOrigNewOrdNo.Trim() != "" && strOrigNewOrdNo.Trim().ToUpper() == strNewOrdNo.Trim().ToUpper())
            {
                blnExistingOrdNo = blnExist;
            }
            return !mblnCancel;
         }

        #region Form Events
        private void btnOk_Click(object sender, EventArgs e)
        {
            if (!ValidCheck()) return;


            mblnCancel = false;
            this.Hide();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            mblnCancel = true;
            this.Hide();
        }
        protected override void ubtLookUp_Click(object sender, EventArgs e)
        {
            string strSQL = "", strFindValue = "", strLookUpField = "", strCaption = "";
            Control con = sender as Control;
            if (con == null) return;
            Control ocon = null;

            DBAccess dbAccess = new DBAccess(false, false);

            switch (con.Name)
            {
                case "ubtOrdNo":

                    strSQL = BaseFunctions.GetLookUpSQL("OeOrderHdr");

                    strCaption = "Order#";
                    strFindValue = txtOrdNo.Text;
                    strLookUpField = "OrdNo";

                    ocon = txtOrdNo;

                    if (strSQL.Trim() == "") return;
                    break;

                case "ubtSupOrCust":
                    strSQL = BaseFunctions.GetLookUpSQL("apsupplier");

                    strCaption = "Supplier";
                    strFindValue = txtSupOrCust.Text;
                    strLookUpField = "SupNo";

                    ocon = txtSupOrCust;
                    if (strSQL.Trim() == "") return;
                    break;


            }

            if (mblnLookUpSortByDescription) strSQL = DataFunctions.GetSqlByDescriptionSort(strSQL);
            mblnLookUpSortByDescription = false;

            frmLookUp frmlookup = new frmLookUp(dbAccess, strSQL, strCaption, strFindValue, strLookUpField);

            frmlookup.ShowDialog();
            if (frmlookup.HaveReturnValue)
            {
                ocon.Text = frmlookup.GetReturnValue(strLookUpField);
                ocon.Focus();
            }
            frmlookup.Close();

            dbAccess.CloseCon();

        }
        private void txtSupOrCust_Leave(object sender, EventArgs e)
        {
            txtSupOrCust.Text = BaseFunctions.FormatWithBlank(txtSupOrCust.Text, 8);
        }
        private void txtOrdNo_Leave(object sender, EventArgs e)
        {
            txtOrdNo.Text = BaseFunctions.FormatWithZero(txtOrdNo.Text, 8);
        }

        #endregion

        private bool ValidCheck()
        {
            string strSql = "";
            DataRow drCheck = null;
            DBAccess dbcon = new DBAccess(false, false);

            switch (mintModule)
            {
                case (int)mModuleType.PurchaseOrdNo:
                    
                    if (txtOrdNo.Text.Trim()=="")
                    {
                        MessageBox.Show(Msg.Select("Order number cannot be empty!", DataFunctions.GetMsg(this.Name, "MSG_OrderNoCanNotBeEmpty")), SysVar.WarnTitle);
                        return false;
                    }

                    if (mstrSupOrCustNo.Trim()=="")
                    {
                        if (!DataFunctions.CheckRecordExist(dbcon, "apsupplier", txtSupOrCust.Text ,"supNo"))
                        {
                            MessageBox.Show(Msg.Select("Supplier not on file!", DataFunctions.GetMsg(this.Name, "MSG_SupplierNotOnFile")), SysVar.WarnTitle);
                            return false;
                        }
                    }

                    mblnExistingOrdNo = false;
                    strSql = "select ordno,supno,posted,void from poorderhdr where OrdNo='" + txtOrdNo.Text + "'";
                    drCheck = dbcon.GetDataFirstRow(strSql);

                    if (drCheck != null)
                    {
                        mblnExistingOrdNo = true;
                        if (mstrSupOrCustNo.Trim() != "")
                        {
                            if (drCheck["supno"].ToString().Trim().ToUpper() != mstrSupOrCustNo.Trim().ToUpper())
                            {
                                MessageBox.Show(Msg.Select("P/O order number already on file for other supplier!", DataFunctions.GetMsg(this.Name, "MSG_POOrderNoAlreadyOnFile")), SysVar.WarnTitle);
                                return false;
                            }
                        }
                        else
                        {
                            if (drCheck["supno"].ToString().Trim().ToUpper() != txtSupOrCust.Text.Trim().ToUpper())
                            {
                                MessageBox.Show(Msg.Select("P/O order number already on file for other supplier!", DataFunctions.GetMsg(this.Name, "MSG_POOrderNoAlreadyOnFile")), SysVar.WarnTitle);
                                return false;
                            }
                        }

                        if (bool.Parse(drCheck["Posted"].ToString()))
                        {
                            MessageBox.Show(Msg.Select("P/O order number already posted!", DataFunctions.GetMsg(this.Name, "MSG_POOrderNoAlreadyPosted")), SysVar.WarnTitle);
                            return false;
                        }

                        if (bool.Parse(drCheck["void"].ToString()))
                        {
                            if (DataFunctions.GetmSwitchValue(dbcon, "ReuseVoidedPO", "po"))
                            {
                                if (MessageBox.Show(Msg.Select("The order# is used by a voided order. Are you sure to reuse it?", DataFunctions.GetMsg(this.Name, "MSG_OrderNoIsUsed")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                                {
                                   return false;
                                }
                                else
                                {
                                    return false;
                                }
                            }
                            else
                            {
                                MessageBox.Show(Msg.Select("P/O order number already void.", DataFunctions.GetMsg(this.Name, "MSG_POOrderNoAlreadyVoid")), SysVar.WarnTitle);
                                return false;
                            }
                        }
                    }
                    
                    break;

                case (int)mModuleType.SalesOrderNo:
                    if (txtOrdNo.Text.Trim() == "")
                    {
                        MessageBox.Show(Msg.Select("O/E order number cannot be empty!", DataFunctions.GetMsg(this.Name, "MSG_OEOrderNoCanNotBeEmpty")), SysVar.WarnTitle);
                        return false;
                    }

                    mblnExistingOrdNo = false;

                    strSql = "select ordno,custno,posted,void from oeorderhdr where ORdNo='" + txtOrdNo.Text + "'";
                    drCheck = dbcon.GetDataFirstRow(strSql);

                    if (drCheck != null)
                    {
                        mblnExistingOrdNo = true;
                        if (drCheck["CustNo"].ToString().Trim().ToUpper() != mstrSupOrCustNo.Trim().ToUpper())
                        {
                            MessageBox.Show(Msg.Select("O/E order number already on file for other customer!", DataFunctions.GetMsg(this.Name, "MSG_OEOrderNoAlreadyOnFile")), SysVar.WarnTitle);
                            return false;
                        }

                        if (bool.Parse(drCheck["Posted"].ToString()))
                        {
                            MessageBox.Show(Msg.Select("O/E order number already posted!", DataFunctions.GetMsg(this.Name, "MSG_OEOrderNoAlreadyPosted")), SysVar.WarnTitle);
                            return false;
                        }

                        if (bool.Parse(drCheck["void"].ToString()))
                        {
                            if (DataFunctions.GetmSwitchValue(dbcon,"ReuseVoidedOE", "oe"))
                            {
                                if (MessageBox.Show(Msg.Select("The order# is used by a voided order. Are you sure to reuse it?", DataFunctions.GetMsg(this.Name, "MSG_OrderNoIsUsed")), SysVar.ConfirmTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                                {
                                    return false;
                                }
                                else
                                {
                                    return false;
                                }
                            }
                            else
                            {
                                MessageBox.Show(Msg.Select("O/E Order number already void.", DataFunctions.GetMsg(this.Name, "MSG_OEOrderNoAlreadyVoid")), SysVar.WarnTitle);
                                return false;
                            }
                        }

                    }

                    if (DataFunctions.CheckRecordExist(dbcon, "oeReturnhdr", txtOrdNo.Text, "ReturnNo"))
                    {
                        MessageBox.Show("O/E order number already on file!", SysVar.WarnTitle);
                        return false;
                    }
                    

                    break;

            case (int)mModuleType.DailyRoute:

                    if (txtOrdNo.Text.Trim()=="")
                    {
                        MessageBox.Show(Msg.Select("O/E order number not on file!", DataFunctions.GetMsg(this.Name, "MSG_OEOrderNoNotOnFile")), SysVar.WarnTitle);
                        return false;
                    }

                    if (!DataFunctions.CheckRecordExist(dbcon, "oeorderhdr", txtOrdNo.Text,"OrdNo"))
                    {
                        MessageBox.Show(Msg.Select("O/E order number not on file!", DataFunctions.GetMsg(this.Name, "MSG_OEOrderNoNotOnFile")), SysVar.WarnTitle);
                        return false;
                    }
                    break;
            }

            dbcon.CloseCon();
            return true;
        }

    }
}